//
//  MyAssetsViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/15.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "MyAssetsViewController.h"
#import "MyAssetsViewTableViewCell.h"
#import "MyAssetsView.h"
#import "TopUpViewController.h"

@interface MyAssetsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UIView *mainTHeaderView;
@property (nonatomic, strong) UIView *mainTFooterView;
@property (nonatomic, assign) int currentPage;
@property (nonatomic, strong) NSMutableArray *datamuArray;
@property (nonatomic, strong) UILabel *yeNumlabel;// 余额
@property (nonatomic, strong) NSString *txMoneyStr; // 想提现的金额
@end

@implementation MyAssetsViewController
// 获取余额 及明细
- (void)mineMoneyList {
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=mymoney" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"error_code"]];
        if ([str isEqualToString:@"0"]) {
            self.datamuArray = responseObject[@"data"][@"detail"];
            self.yeNumlabel.text = responseObject[@"data"][@"money"];
            [self.mainTableView reloadData];
        }else{
            ShowMessage(@"操作失败");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
// 提现
- (void)giveMineMoney {
    if ([self.txMoneyStr isEqualToString:@"0"]) {
        ShowMessage(@"输入金额有误");
        return;
    }
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=withdraw" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token,@"amount":self.txMoneyStr} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"error_code"]];
        if ([str isEqualToString:@"0"]) {
            ShowMessage(@"提现申请成功");
            [self mineMoneyList];
        }else{
            ShowMessage(responseObject[@"message"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
    }];
}
- (void)btnAction:(UIButton *)btn
{
    NSLog(@"提现");
    MyAssetsView *myAssetsView = [[MyAssetsView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    myAssetsView.moneyField.text = self.yeNumlabel.text;
    __weak typeof(self)weakself = self;
    __weak typeof(myAssetsView)weakMyAssetsView = myAssetsView;
    myAssetsView.txBtnBlock = ^(NSString *numStr) {
        weakself.txMoneyStr = numStr;
        [weakself giveMineMoney];
        [weakMyAssetsView hidden];
    };
    [myAssetsView show];
}
- (void)czBtnAction:(UIButton *)btn
{
    NSLog(@"充值");
    TopUpViewController *VC = [[TopUpViewController alloc]init];
    [self.navigationController pushViewController:VC animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"我的资产";
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self mineMoneyList];
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datamuArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyAssetsViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyAssetsViewTableViewCell class])];
    cell.dataDic = self.datamuArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
#pragma mark -- init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = [UIColor clearColor];
        _mainTableView.tableHeaderView = self.mainTHeaderView;
        _mainTableView.tableFooterView = self.mainTFooterView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[MyAssetsViewTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MyAssetsViewTableViewCell class])];
        
//        self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            self.currentPage = 0;
////            [self getDiscoverDataInformation];
//        }];
//        self.mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//            self.currentPage++;
////            [self getDiscoverDataInformation];
//        }];
//        [self.mainTableView.mj_header beginRefreshing];
    }
    return _mainTableView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = getColor(whiteColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight)];
        _headerView.backgroundColor = getColor(whiteColor);
        // 上面View
        UIView *topbgViwe = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 / HEIGHT_6S_SCALE)];
        topbgViwe.backgroundColor = getColor(@"4A4C5B");
        [_headerView addSubview:topbgViwe];
        
        UILabel *yelabel = [[UILabel alloc] initWithFrame:CGRectMake(20 / WIDTH_6S_SCALE, 15 / HEIGHT_6S_SCALE, 80 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
        yelabel.font = DEF_FontSize_12;
        yelabel.textColor = getColor(whiteColor);
        yelabel.textAlignment = NSTextAlignmentLeft;
        yelabel.text = @"余额（元）";
        [topbgViwe addSubview:yelabel];
        
        UIButton *czBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yelabel.frame) + 20 / WIDTH_6S_SCALE, 15 / HEIGHT_6S_SCALE, 60 / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
        czBtn.backgroundColor = [UIColor clearColor];
        [czBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [czBtn setTitle:@"充值" forState:UIControlStateNormal];
        czBtn.titleLabel.font = DEF_FontSize_16;
        [czBtn addTarget:self action:@selector(czBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topbgViwe addSubview:czBtn];
        czBtn.hidden = YES;
        
        UILabel *yeNumlabel = [[UILabel alloc] initWithFrame:CGRectMake(20 / WIDTH_6S_SCALE, CGRectGetMaxY(yelabel.frame) + 10, 200 / WIDTH_6S_SCALE, 30 / HEIGHT_6S_SCALE)];
        yeNumlabel.font = DEF_FontSize_26;
        yeNumlabel.textColor = getColor(whiteColor);
        yeNumlabel.textAlignment = NSTextAlignmentLeft;
        yeNumlabel.text = @"0.00";
        self.yeNumlabel = yeNumlabel;
        [topbgViwe addSubview:yeNumlabel];
        
        
        // 中间View
        UIView *middlebgViwe = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topbgViwe.frame), SCREEN_WIDTH, 388 / HEIGHT_6S_SCALE)];
        middlebgViwe.backgroundColor = getColor(@"F0F0F0");
        [_headerView addSubview:middlebgViwe];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 / WIDTH_6S_SCALE, -28 / HEIGHT_6S_SCALE, SCREEN_WIDTH - 20 / WIDTH_6S_SCALE, 56 / HEIGHT_6S_SCALE)];
        label.font = DEF_FontSize_14;
        label.backgroundColor = getColor(whiteColor);
        label.textColor = getColor(blackColor);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"    余额明细";
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
        [middlebgViwe addSubview:label];
        
        [middlebgViwe addSubview:self.mainTableView];
        [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(label.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.right.equalTo(middlebgViwe);
            make.bottom.equalTo(middlebgViwe.mas_bottom);
        }];
        
        
        UIView *bottombgViwe = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(middlebgViwe.frame), SCREEN_WIDTH, 65 / HEIGHT_6S_SCALE)];
        bottombgViwe.backgroundColor = [UIColor whiteColor];
        [_headerView addSubview:bottombgViwe];
        UIButton *txBtn = [[UIButton alloc]init];
        txBtn.backgroundColor = getColor(mainColor);
        [txBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        txBtn.titleLabel.font = DEF_FontSize_14;
        [txBtn setTitle:@"提现" forState:UIControlStateNormal];
        [txBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [bottombgViwe addSubview:txBtn];
        [txBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bottombgViwe.mas_left).offset(10 / WIDTH_6S_SCALE);
            make.right.equalTo(bottombgViwe.mas_right).offset(-10 / WIDTH_6S_SCALE);
            make.top.equalTo(bottombgViwe.mas_top).offset(10 / WIDTH_6S_SCALE);
            make.bottom.equalTo(bottombgViwe.mas_bottom).offset(-10 / WIDTH_6S_SCALE);
        }];
        txBtn.layer.cornerRadius = 5;
        txBtn.layer.masksToBounds = YES;
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (UIView *)mainTHeaderView
{
    if (!_mainTHeaderView) {
        _mainTHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _mainTHeaderView;
}
- (UIView *)mainTFooterView
{
    if (!_mainTFooterView) {
        _mainTFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _mainTFooterView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
